cmake_minimum_required(VERSION 3.8)
project(libstmconn)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 查找依赖
find_package(ament_cmake REQUIRED)

# 添加package modules path
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")

# 包含em_expand宏
include(em_expand)

# 包含头文件目录
include_directories(
  include
  ${CMAKE_CURRENT_BINARY_DIR}/generated/include
)

# 使用em_expand宏生成源代码
em_expand(${CMAKE_CURRENT_SOURCE_DIR}/stm.context.py.in
  ${CMAKE_CURRENT_BINARY_DIR}/generated/stm.context.py
  ${CMAKE_CURRENT_SOURCE_DIR}/include/stmconn/stm_dialect.hpp.em
  ${CMAKE_CURRENT_BINARY_DIR}/generated/include/stmconn/stm_dialect.hpp)

em_expand(${CMAKE_CURRENT_SOURCE_DIR}/stm.context.py.in
  ${CMAKE_CURRENT_BINARY_DIR}/generated/stm.context.py
  ${CMAKE_CURRENT_SOURCE_DIR}/src/stm_helpers.cpp.em
  ${CMAKE_CURRENT_BINARY_DIR}/generated/src/stm_helpers.cpp)

# 创建库请先仔细学习这个目录下文件极其架构
add_library(${PROJECT_NAME}
  src/stmconn_interface.cpp
  src/stmconn_serial.cpp
  src/stmconn_mock.cpp
  ${CMAKE_CURRENT_BINARY_DIR}/generated/src/stm_helpers.cpp
)

# 设置库的包含目录
ament_export_include_directories(include)

# 导出库
ament_export_libraries(${PROJECT_NAME})

# 安装头文件
install(
  DIRECTORY include/
  DESTINATION include
)

# 安装生成的头文件
install(
  DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated/include/stmconn/
  DESTINATION include/stmconn
  FILES_MATCHING PATTERN "*.hpp"
)

# 安装cmake模块
install(
  DIRECTORY cmake/
  DESTINATION share/${PROJECT_NAME}
)

# 安装库
install(
  TARGETS ${PROJECT_NAME}
  EXPORT export_${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)

ament_package()